< previous page page_121 next page >

Page 121
This code simply assigns 1 to a variable named intLoop. If this variable does not exist, Visual Basic will create it just before performing the assignment. However, the preceding code will not compile if the words Option Explicit appear in the (General) (Declarations) section of the module where this code resides.
Examples Filename: Typos.bas
Creating variables on-the-fly has a couple drawbacks. First, the variable will be declared as a Variant data type. A variant takes a lot of memory to store. Its best not to use one if you can help it. Second, if you dont declare your variables, a simple typo can create a new variable, where you thought you were using another variable. This can lead to a nasty bug to track down. Therefore, always declare your variables prior to using them and be sure Option Explicit appears in the (General) (Declarations) section of each module.
EXERCISE
Be Careful with Mistyped Variables
1. Look at the following code and answer the questions that follow:
Public Sub CalcValue()
Dim intVar1 As Integer
Dim intValue As Integer

intVar1 = 10
intValue = 20

intVar1 = intValue + 50
intValue = intValue + 10
intVor1 = intValue - 50

MsgBox "intVar1 = " & intValue1
MsgBox "intValue = " & intValue

End Sub
2. What is the value of intVar1?
3. What is the value of intValue?

 
< previous page page_121 next page >

If you like this book, buy it!